package com.hualan.sfzy.controller.counselor;

import com.hualan.sfzy.entity.counselor.Counselor ;
import com.hualan.sfzy.service.counselor.CounselorService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CounselorController {
    @Autowired
    private CounselorService counselorService;

    @PostMapping("/counselor")
    public Result add(@RequestBody Counselor counselor){
        Counselor savedCounselor = counselorService.add(counselor);
        return Result.success(savedCounselor);
    }

    @DeleteMapping("/counselor/{id}")
    public Result remove(@PathVariable Integer id){
        int num = counselorService.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/counselor")
    public Result removeBatch(Integer[] ids){
        int num = counselorService.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();
    }

    @PutMapping("/counselor")
    public Result edit(@RequestBody Counselor counselor){
        int num = counselorService.edit(counselor);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/counselor/{id}")
    public Result findById(@PathVariable Integer id){
        Counselor counselor = counselorService.findById(id);
        return Result.success(counselor);
    }

    @GetMapping("/counselor/page")
    public Result findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize,Counselor counselor){
        Map<String, Object> map = counselorService.findByPage(pageNum, pageSize, counselor);

        return Result.success(map);
    }

    @GetMapping("/counselor")
    public Result find(){
        List<Counselor> list = counselorService.find();
        return Result.success(list);
    }

    @GetMapping("/counselor1")
    public Result findc(){
        List<Counselor> list = counselorService.findc();
        return Result.success(list);
    }

    @GetMapping("/counselor/sift")
    public Result sift(){
        HashMap<String, Object> map = counselorService.sift();
        return Result.success(map);
    }

    @PostMapping("/counselor/filter")
    public Result filter(@RequestBody Counselor counselor){
        List<Counselor> list = counselorService.findFilter(counselor);
        return Result.success(list);
    }

}
